var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
var app = getApp();

Page({
  data: {
    activeList: [],
    status: 0,
    page: 1,
    size: 10,
    count: 0,
    scrollTop: 0,
    showPage: false
  },

  onLoad: function (options) {
    this.getCouponList();
  },

  onReady: function () {},

  onShow: function () {},

  onHide: function () {},

  onUnload: function () {},

  onPullDownRefresh: function () {},

  onReachBottom: function () {},

  onShareAppMessage: function () {},

  getCouponList: function () {
    let that = this;
    that.setData({
      scrollTop: 0,
      showPage: false,
      activeList: []
    });
    util.request(api.ActiveMyList, {
      status: that.data.status,
      page: that.data.page,
      size: that.data.size
    }).then(function (res) {
      if (res.errno === 0) {
        that.setData({
          scrollTop: 0,
          activeList: res.data.data,
          showPage: true,
          count: res.data.count
        });
      }
    });

  },

  nextPage: function (event) {
    var that = this;
    if (this.data.page > that.data.count / that.data.size) {
      return true;
    }

    that.setData({
      page: that.data.page + 1
    });

    this.getCouponList();

  },
  prevPage: function (event) {
    if (this.data.page <= 1) {
      return false;
    }

    var that = this;
    that.setData({
      page: that.data.page - 1
    });
    this.getCouponList();
  },
  switchTab: function (e) {
    this.setData({
      couponList: [],
      status: e.currentTarget.dataset.index,
      page: 1,
      size: 10,
      count: 0,
      scrollTop: 0,
      showPage: false
    });

    this.getCouponList();
  },
})